//
//  WBStatusViewModel.swift
//  Weibo
//
//  Created by yinbo on 2018/3/19.
//  Copyright © 2018年 yinbo. All rights reserved.
//

import Foundation
//单条微博的视图模型
/**
 如果没有任何父类，希望在开发时调试，输出调试信息，需要:
 1.遵守 CustomStringConvertible协议
 2.实现 description 计算型属性
 
 关于表格的性能优化:
 1.尽量少计算，所有需要的素材提前计算好
 2.控件上不要设置圆角半径，所有图像渲染的属性，都要注意
 3.不要动态创建控件，所有需要的控件都要提前计算好，在显示的时候根据数据显示/隐藏
 4.Cell 中控件的层数越少越好，数量越少越好
 */
class WBStatusViewModel:CustomStringConvertible{
    //微博模型
    var status:WBStatus
    //会员图标
    var memberIcon:UIImage?
    
    //认证类型
    var vipIcon:UIImage?
    //转发文字
    var retweetedStr:String?
    var commentStr:String?
    var likeStr:String?
    //被转发微博文字
    var retweetedText:String?
    var rowHeigth:CGFloat=0
    var  picURLs:[WBStatusPicture]?{
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    //配图视图大小
    var pictureViewSize = CGSize()
    init(model:WBStatus) {
        self.status = model
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7{
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            memberIcon = UIImage(named:imageName)
        }
        //认证图标
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
        retweetedStr = countString(count: model.reposts_count, defaultStr: "转发")
        commentStr = countString(count: model.comments_count, defaultStr: "评论")
        likeStr = countString(count: model.attitudes_count, defaultStr:"赞")
        //计算配图视图大小
        pictureViewSize = calcPictureViewSize(count:picURLs?.count)
        
        retweetedText = "@"+(status.retweeted_status?.user?.screen_name ?? "")+":"+(status.retweeted_status?.text ?? "")
        updateRowHeight()
        
    }
    
    //计算行高
    func updateRowHeight(){
        let margin:CGFloat = 12
        let iconHeight:CGFloat = 34
        let toolbarHeight:CGFloat = 35
        var heigth:CGFloat = 0
        //设置正文文本预期尺寸，宽度固定，高度尽量大
        let viewSize = CGSize(width: UIScreen.cz_screenWidth()-2*margin, height: CGFloat(MAXFLOAT))
        let originalFont = UIFont.systemFont(ofSize: 17)
        let retweetedFont = UIFont.systemFont(ofSize: 14)
        //1.计算顶部位置
        heigth = 2*margin+iconHeight+margin
        //2.正文高度
        if let text = status.text{
            heigth += (text as NSString).boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName:originalFont], context: nil).height
        }
        if status.retweeted_status != nil{
            heigth += 2*margin
            if let text = retweetedText{
                heigth += (text as NSString).boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName:retweetedFont], context: nil).height
            }
        }
        //配图视图
        heigth += pictureViewSize.height
        heigth += margin
        heigth += toolbarHeight
        rowHeigth = heigth
    }
    
    private func calcPictureViewSize(count: Int?) -> CGSize{
        
        guard let count = count else {
            return CGSize()
        }
        
        if count == 0 {
            return CGSize()
        }
        //根据count计算行数
        let row = (count - 1) / 3 + 1
        //根据行数计算高度
        let height = WBStatusPictureViewOutterMargin + CGFloat(row) * WBStatusPictureItemWidth + CGFloat(row - 1) * WBStatusPictureViewInnerMargin
        
        return CGSize(width: WBStatusPictureViewWidth, height: height)
        
    }
    func countString(count:Int,defaultStr:String)->String{
        if count == 0{
            return defaultStr
        }
        if count < 1000{
            return count.description
        }
        return String(format:"%.02f万",Double(count)/1000)
    }
    var description: String{
        return status.yy_modelDescription()
    }
    func updateSingleImageSize(image:UIImage){
        var size = image.size
        //过宽图片处理
        let maxWidth:CGFloat = 300
        let minWidth:CGFloat = 40
        if size.width>maxWidth{
            size.width = maxWidth
            size.height = size.width*image.size.height/image.size.width
        }
        if size.width<minWidth{
            size.width = minWidth
            size.height = size.width*image.size.height/image.size.width/4
        }
        //特例：有些图像，本身就是很窄很长--->定义一个minHeight，思路同上
        size.height += WBStatusPictureViewOutterMargin
        pictureViewSize = size
        updateRowHeight()
    }
    
}
 
